package com.zc.app.sys.controller;

import com.zc.app.sys.dao.peer.SystemRoleMenuPeer;
import com.zc.app.sys.dao.peer.SystemUserPeer;
import com.zc.app.sys.dao.peer.SystemUserRolePeer;
import com.zc.app.sys.service.*;
import com.zc.framework.common.bean.AjaxResult;
import com.zc.framework.common.bean.MyPageInfo;
import com.zc.framework.system.BaseController;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.lang.reflect.InvocationTargetException;

/**
 * Created by zc on 2016/12/19.
 */
@Controller
@RequestMapping("/sys/user/role")
public class SystemUserRoleController extends BaseController {

    private static Logger logger = LogManager.getLogger(SystemUserRoleController.class);

    @Autowired
    private SystemUserRoleService systemUserRoleService;

    @Autowired
    private SystemUserService systemUserService;

    @InitBinder({ "sysUserRoleQuery" })
    public void initBinderQuery(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("sysUserRoleQuery.");
    }

    @InitBinder({ "sysUserRole" })
    public void initBinderSysUserRole(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("sysUserRole.");
    }

    @RequestMapping(value = "/index", method = {RequestMethod.GET})
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("/admin/sys/role/user_role");
        return modelAndView;
    }

    @RequestMapping(value = "/list", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public MyPageInfo<SystemUserRolePeer> search(@ModelAttribute("sysUserRoleQuery")SystemUserRolePeer userRolePeer) throws InvocationTargetException, IllegalAccessException {
        if(userRolePeer==null){
            userRolePeer = new SystemUserRolePeer();
        }
        MyPageInfo<SystemUserRolePeer> pageInfo = systemUserRoleService.queryPages(userRolePeer, userRolePeer.getPage(),userRolePeer.getRows());
        logger.info("pageInfo:"+pageInfo);
        return pageInfo;
    }

    @RequestMapping(value = "/edit", method = {RequestMethod.GET})
    public ModelAndView edit(@RequestParam(value = "pageType",required = false)String pageType,
                             @RequestParam(value = "userRoleId",required = false)Integer userRoleId){
        ModelAndView modelAndView = new ModelAndView("/admin/sys/role/user_role_edit");
        SystemUserRolePeer userRolePeer = new SystemUserRolePeer();
        if("edit".equals(pageType)
                && userRoleId!=null){
            userRolePeer = systemUserRoleService.selectByPrimaryKey(userRoleId);
        }
        modelAndView.addObject("sysUserRole", userRolePeer);
        modelAndView.addObject("pageType",pageType);
        return modelAndView;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult login(@ModelAttribute("sysUserRole") SystemUserRolePeer userRolePeer,
                            @RequestParam(value = "pageType",required = false)String pageType) {


        if("edit".equals(pageType)){
            if (userRolePeer.getUserRoleId()==null) {
                return fail(false, "ID不能为空!");
            }
            systemUserRoleService.updateByPrimaryKey(userRolePeer);
            return success(true);
        }

        if (userRolePeer.getRoleId()==null) {
            return fail(false, "角色不能为空!");
        }
        if (userRolePeer.getAccountId()==null) {
            return fail(false, "用户不能为空!");
        }

        SystemUserPeer currUser = systemUserService.getCurrentUser();
        userRolePeer.setCreateBy(currUser.getUserName());
        systemUserRoleService.createUserRole(userRolePeer);
        return success(true);
    }

}
